class Solution1 {
    public long countSubarrays(int[] nums, int k) {
        int n=nums.length;
        int left=0;
        int right=0;
        long ans=0;
        int mx=0;
        int count=0;

        //找到最大值
        for (int i = 0; i < n; i++) {
            mx=Math.max(mx,nums[i]);
        }

        while(right<n){
            int x=nums[right];
            if(x==mx){
                count++;
            }
            while(count==k){
                int t=nums[left];
                if(t==mx){
                    count--;
                }
                left++;

            }
            ans+=left;
            right++;
        }
        return ans;
    }
}